package com.lins.storedisk.bo.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.lins.disk.bo.UserBo;
import com.lins.disk.common.APIException;
import com.lins.disk.common.AppCode;
import com.lins.disk.constant.MyEnum;
import com.lins.disk.dao.UserDao;
import com.lins.disk.dto.LoginDTO;
import com.lins.disk.dto.RegisterDTO;
import com.lins.disk.entity.UserEntity;
import com.lins.disk.utils.MyIpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author linshuo
 * @Date 2023/12/1 10:47
 */
@Slf4j
@Service
public class UserBoImpl implements UserBo {

    @Autowired
    UserDao userDao;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public Integer regUser(RegisterDTO dto) {
        //校验两次密码是否一致
        if (!dto.getPassword().equals(dto.getPassword2())) throw new APIException(AppCode.PWD_ERROR,"两次密码不一致");

        //检查图形验证码
//        YzmImageUtil.checkVerification(dto.getVerification());

        //校验短信验证码
//        MsmUtil.checkMsmCode(dto.getPhoneNum(),dto.getMsmCode(),redisTemplate);

        //查重
        checkDuplicateRegister(dto);

        //入库
        UserEntity user = new UserEntity(
                dto.getUsername(),
                dto.getPassword(),
                dto.getPhoneNum(),
                MyEnum.OFF_LINE.getValue()
        );
        UserEntity entity = userDao.save(user);
        log.info("用户{}注册成功：{}",entity.getUsername(),entity.getId());
        return 1;
    }

    @Override
    public Map<String,String> doLogin(LoginDTO dto) {

        //先检查图形验证码
//        YzmImageUtil.checkVerification(dto.getVerification()); //跨域丢失sessionID

        UserEntity user = userDao.findByUsernameOrPhone(dto.getUsername(), dto.getUsername());

        //查无此人
        if (ObjectUtil.isEmpty(user)) throw new APIException(AppCode.USERNAME_ERROR,"请核对");

        //密码不对
        if (!dto.getPassword().equals(user.getPassword())) throw new APIException(AppCode.PWD_ERROR,"请核对");

        //认证 并把user存redis
        StpUtil.login(user.getId());

        //响应数据
        List<String> ipAndHost = MyIpUtil.getIpAndHost(dto.getRequest());
        String requestIp = ipAndHost.get(0);
        String requestHost = ipAndHost.get(1);
        String clientIp = dto.getClientIp();
        String clientArea = dto.getClientArea();

        //登录日志记录到时序库，1 AOP   2 通过log

        //临时构建响应数据对象
        Map<String,String> map = new LinkedHashMap<>();
//        map.put("requestIp",requestIp);
//        map.put("requestHost",requestHost);
//        map.put("clientIp",clientIp==null?"":clientIp);
//        map.put("clientArea",clientArea==null?"":clientArea);
        map.put("userID",user.getId().toString());
        map.put("token",StpUtil.getTokenValue());
        map.put("tokenName",StpUtil.getTokenName());
        map.put("tokenTimeout",StpUtil.getTokenTimeout()+"秒");
//        map.put("tokenInfo",StpUtil.getTokenInfo().toString());
        return map;
    }


    //注册时查重
    public void checkDuplicateRegister(RegisterDTO dto){
        UserEntity user = userDao.findByUsernameOrPhone(dto.getUsername(), dto.getPhoneNum());
        if (user != null) {
            if (dto.getUsername().equals(user.getUsername())){
                throw new APIException(AppCode.REGISTERUSER_ERROR,
                        "你尝试注册的用户名“" + dto.getUsername() + "”已经被占用，请更换用户名后重试");
            }
            if (dto.getPhoneNum().equals(user.getPhone())){
                throw new APIException(AppCode.REGISTERUSER_ERROR,
                        "你的手机号“" + dto.getUsername() + "”已经被使用，请更换手机号后重试");
            }
        }
    }


}
